#!/usr/bin/env bash
# Wrapper for magichue.py with Luna-approved presets

set -e

## Dependency checking
function installed() { command -v "$1" >/dev/null 2>&1; }
function die() { >&2 echo "Fatal: ${@}"; exit 1; }

deps=(magichue.py)
for dep in "${deps[@]}"; do
    installed "${dep}" || die "Missing '${dep}'. Ensure ${dep} is in \$PATH"
done

## Constants
LIGHT_IP_ADDR="192.168.1.138"

## Main program
function set_preset() {
    case "${preset}" in
        vaporwave)
            magichue.py -ip "${LIGHT_IP_ADDR}" -raw 61:60:10:0f
            ;;
        *)
            die "ERROR: preset ${preset} not valid."
            ;;
    esac
}

function set_color() {
    case "${color}" in
        blue)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 0,0,255
            ;;
        cyan)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 0,255,255
            ;;
        magenta)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 255,0,255
            ;;
        green)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 0,255,0
            ;;
        red)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 255,0,0
            ;;
        orange)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 255,127,0
            ;;
        yellow)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 255,255,0
            ;;
        white)
            magichue.py -ip "${LIGHT_IP_ADDR}" -rgb 255,255,255
            ;;
        *)
            die "ERROR: color ${color} is not valid."
            ;;
    esac
}

function toggle_power() {
    case "${power}" in
        off) # If off, turn on
            magichue.py -ip "${LIGHT_IP_ADDR}" -raw 71:23:0f
            ;;
        on)  # If on, turn off
            magichue.py -ip "${LIGHT_IP_ADDR}" -raw 71:24:0f
            ;;
        *)
            die 'ERROR: power status unknown'
            ;;
    esac
}

## Options
power="$(magichue.py -ip 192.168.1.138 -status | cut -d\" -f4)"
preset=
color=

while :; do
    case "${1}" in
        -p|--preset)
            if [[ "${2}" ]]; then
                preset="${2}"
                set_preset
                shift
            else
                die 'ERROR: "--preset" requires a non-empty option argument.'
            fi
            ;;
        --preset=?*)
            preset=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
        --preset=)   # Handle the case of an empty --preset=
            die 'ERROR: "--preset" requires a non-empty option argument.'
            ;;
        -c|--color)
            if [[ "${2}" ]]; then
                color="${2}"
                set_color
                shift
            else
                die 'ERROR. "--color" requires a non-empty option argument.'
            fi
            ;;
        --color=?*)
            preset=${1#*-}
            ;;
        --color=)
            die 'ERROR. "--color" requires a non-empty option argument.'
            ;;
        -t|--toggle)
            toggle_power
            ;;
        --)
            shift
            break
            ;;
        -?*)
            printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
            ;;
        *)
            break
    esac
    shift
done
